package com.huawei.hms.utils;

import android.content.Context;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.log.HMSLog;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public abstract class FileUtil {
    public static final String LOCAL_REPORT_FILE = "hms/HwMobileServiceReport.txt";
    public static final String LOCAL_REPORT_FILE_CONFIG = "hms/config.txt";
    public static final long LOCAL_REPORT_FILE_MAX_SIZE = 10240;
    private static boolean a = false;
    private static ScheduledExecutorService b = Executors.newSingleThreadScheduledExecutor();

    public static void writeFile(final File file, final String str, final long j) {
        b.execute(new Runnable() {
            public void run() {
                Throwable e;
                if (file == null) {
                    HMSLog.e("FileUtil", "In writeFile Failed to get local file.");
                    return;
                }
                File parentFile = file.getParentFile();
                if (parentFile == null || !(parentFile.mkdirs() || parentFile.isDirectory())) {
                    HMSLog.e("FileUtil", "In writeFile, Failed to create directory.");
                    return;
                }
                Closeable closeable = null;
                try {
                    long length = file.length();
                    if (length > j) {
                        String canonicalPath = file.getCanonicalPath();
                        if (!file.delete()) {
                            HMSLog.e("FileUtil", "last file delete failed.");
                        }
                        closeable = new RandomAccessFile(new File(canonicalPath), "rw");
                    } else {
                        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
                        Object obj;
                        try {
                            randomAccessFile.seek(length);
                            obj = randomAccessFile;
                        } catch (IOException e2) {
                            e = e2;
                            obj = randomAccessFile;
                            try {
                                HMSLog.e("FileUtil", "writeFile exception:", e);
                                IOUtils.closeQuietly(closeable);
                            } catch (Throwable th) {
                                e = th;
                                IOUtils.closeQuietly(closeable);
                                throw e;
                            }
                        } catch (Throwable th2) {
                            e = th2;
                            obj = randomAccessFile;
                            IOUtils.closeQuietly(closeable);
                            throw e;
                        }
                    }
                    closeable.writeBytes(str + System.getProperty("line.separator"));
                    IOUtils.closeQuietly(closeable);
                } catch (IOException e3) {
                    e = e3;
                    HMSLog.e("FileUtil", "writeFile exception:", e);
                    IOUtils.closeQuietly(closeable);
                }
            }
        });
    }

    public static void writeFileReport(Context context, File file, File file2, String str, long j, int i) {
        if (file != null && file.isFile() && file.exists()) {
            if (!a) {
                if (!(file2 == null || !file2.exists() || file2.delete())) {
                    HMSLog.e("FileUtil", "file delete failed.");
                }
                a = true;
            }
            writeFile(file2, str + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + j + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + i, LOCAL_REPORT_FILE_MAX_SIZE);
        }
    }
}
